﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.Utilities;

namespace StyleMVVM.Data
{
	public class DataHandle<T, TKey> where T : class
	{
		private T value;
		private TKey key;

		public DataHandle(TKey key)
		{
			this.key = key;
		}

		public bool HasValue
		{
			get { return value != null; }
		}

		public Task<T> GetValue(bool forceGet = false)
		{
			if (value != null && !forceGet)
			{
				return TaskHelper.ReturnTask(value);
			}

			IDataAccessAdapter<T, TKey> accessAdapter = 
				Bootstrapper.Instance.Container.Locate<IDataAccessAdapter<T, TKey>>();

			if (accessAdapter != null)
			{
				return accessAdapter.Get(key);
			}

			throw new Exception("Could not locate class exporting " + typeof(IDataAccessAdapter<T, TKey>).FullName);
		}
	}
}
